<?php

function cURLEnclose($ch, $result, $err) {
	$r = curl_exec($ch);

	if(curl_errno($ch))
	{
		$result = false;
	    $err = array(
		    'no' => curl_errno($ch),
		    'msg' => 'Curl error: ' . curl_error($ch)
		);
	}

	curl_close($ch);
	return $r;
}

function cURLPost($url, $_data, $result, $err) {

	foreach ( $_data as $key => $value) {
		$post_items[] = $key . '=' . $value;
	} 
	$post_string = implode ('&', $post_items);

	$ch = curl_init($url);
 
	//set options
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

	//set data to be posted
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
	 
	return cURLEnclose($ch, &$result, &$err);
}

function cURLGet($url, $_data, $result, $err) {

	foreach ( $_data as $key => $value) {
		$_items[] = $key . '=' . $value;
	} 
	$param_string = implode ('&', $_items);

	if (!empty($param_string))
		$param_string = '?' . $param_string;

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url . $param_string);
 
	//set options
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	
	// handle error if exist.
	return cURLEnclose($ch, &$result, &$err);
}

?>